package cn.hh.ymcc.mq.consumer;

import cn.hh.ymcc.domain.MediaFile;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

@Component
public class MediaFileProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     *  发送消息是否成功
     * @param mediaFile
     * @return
     */
    public Boolean sendMessage(MediaFile mediaFile){
        // 为了方便消费方,重新将message封装MediaFile对象,调用JSON的toJSONString()转成字符串对象mediaFileJsonStr
        String mediaFileJsonStr = JSON.toJSONString(mediaFile);

        // 同步发送消息.返回一个结果对象sendResult    syncSend()
        SendResult sendResult = rocketMQTemplate.syncSend("media-topic:media-tags", mediaFileJsonStr);
        // 结果对象sendResult调用getSendStatus方法()获取结果,在返回最终结果对象sendStatus
        SendStatus sendStatus = sendResult.getSendStatus();
        // 最后return,最终结果对象 == SendStatus枚举中的SEND_OK
        return sendStatus == SendStatus.SEND_OK;  // 发送成功就返回成功,失败就返回失败


    }

}
